package com.xuecheng.content.controller;

import com.xuecheng.commons.model.dto.CourseAuditDto;
import com.xuecheng.commons.model.dto.CourseBaseDto;
import com.xuecheng.commons.model.dto.CourseDto;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.vo.CourseBaseVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.CourseBase;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 * 课程基本信息前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
@RequestMapping("/course")
public class CourseBaseController {

    @Autowired
    private CourseBaseService  courseBaseService;

    //查询列表
    @PostMapping("list")
    public ResponseResult list (PageRequest request, @RequestBody CourseDto dto) {
        return courseBaseService.courseList(request,dto);
    }

    //上传封面图片
    @PostMapping("upload")
    public ResponseResult upload (MultipartFile uploadFile) throws Exception {
        return courseBaseService.upload(uploadFile);
    }

    //保存课程
    @PostMapping
    public ResponseResult saveCourseBase (@RequestBody CourseBaseDto dto) {
        return courseBaseService.saveCourseBase(dto);
    }

    /**
     * 根据课程id查询课程
     */
    @GetMapping("{courseBaseId}")
    public ResponseResult findById (@PathVariable("courseBaseId") Long courseBaseId) {
        CourseBase courseBase = courseBaseService.getById(courseBaseId);
        CourseBaseVo vo = BeanHelper.copyProperties(courseBase, CourseBaseVo.class);
        return ResponseResult.okResult(vo);
    }

    @PutMapping
    public ResponseResult updateCourseBase (@RequestBody CourseBaseDto dto) {
        return courseBaseService.updateCourseBase(dto);
    }

    //根据课程id删除课程
    @DeleteMapping("{courseId}")
    public ResponseResult deleteCourseBase (@PathVariable("courseId") Long courseId) {
        return courseBaseService.deleteCourseBase(courseId);
    }

    //机构提交课程
    @GetMapping("commit/{courseBaseId}")
    public ResponseResult commit (@PathVariable("courseBaseId") Long courseBaseId) {
        return courseBaseService.commit(courseBaseId);
    }

    //管理员审核课程
    @PostMapping("approve")
    public ResponseResult approve (@RequestBody CourseAuditDto dto) {
        return courseBaseService.approve(dto);
    }

    //已发布课程下架
    @GetMapping("offline/{courseId}")
    public ResponseResult offline (@PathVariable("courseId") Long courseId) throws Exception {
        return courseBaseService.offline(courseId);
    }
}
